home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / aijournl / 1986_09 / turbo1.ltg < prev    next >
Text File  |  1986-10-30  |  2KB  |  70 lines

  1.  
  2. Turbo PROLOG:
  3. Backtracking/Unification Benchmark
  4.  
  5.  
  6.  
  7. /* Test CPU-intensive unification and backtracking by generating and reversing
  8.    lots of lists. */
  9.  
  10.  
  11. DOMAINS
  12.  
  13. LIST = INTEGER*            /* LIST will stand for a list of INTEGERs */
  14.  
  15.  
  16. PREDICATES
  17.  
  18. append(LIST,LIST,LIST)
  19. reverse(LIST,LIST)
  20. makelist(INTEGER,LIST)
  21. test2(INTEGER,INTEGER)
  22. test(INTEGER)
  23.  
  24.  
  25. CLAUSES
  26.  
  27. /* Append first argument to second, returning result via third argument.
  28.    All arguments must be lists. */
  29.  
  30. append([],L,L) :- !.
  31. append([H|L1],L2,[H|L3]) :- append(L1,L2,L3).
  32.  
  33.  
  34. /* Reverse first list argument and return result in second argument. */
  35.  
  36. reverse([],[]) :- !.
  37. reverse([H|T],RevL) :- reverse(T,RevT), append(RevT,[H],RevL).
  38.  
  39.  
  40. /* Make a list of integers.  First argument specifies number of elements,
  41.    second argument returns the list.  List elements are generated in descending
  42.    numerical order. */
  43.  
  44. makelist(0,[]) :- !.
  45. makelist(N,[N|L]) :- M = N - 1, makelist(M,L).
  46.  
  47.  
  48. /* The heart of the benchmark:  using a tail recursive loop, generate and
  49.    reverse lists starting at length N up to length Limit.  */
  50.  
  51. test2(Limit,Limit) :- !.
  52. test2(N,Limit) :-  makelist(N,L), reverse(L,_), M = N + 1, test2(M,Limit).
  53.  
  54.  
  55. /* Shorthand to call the test2 predicate with the starting list length
  56.    defaulting to 2.  NOTE!!!  Turbo Prolog doesn't allow two predicates
  57.    of the same name and different arities, which is why test and test2
  58.    have different names. */
  59.  
  60. test(Limit) :- test2(2,Limit).
  61.  
  62.  
  63. GOAL
  64.  
  65. /* Prompt for the desired limit and run the benchmark. */
  66.  
  67. nl, write(ready), nl, readint(Limit), test(Limit), write(done), nl.
  68.  
  69.  
  70.